package gold.digger;

/**
 * Created by fanzhenyu02 on 2020/3/1.
 */
public class LC152 {


    public int maxProduct(int[] arr) {
        int maxProduct = arr[0];
        int preMax = arr[0], preMin = arr[0], curMax, curMin;

        for (int i = 1; i < arr.length; i++) {
            if (arr[i] >= 0) {
                curMax = Math.max(arr[i], preMax * arr[i]);
                curMin = Math.min(arr[i], preMin * arr[i]);
            } else {
                curMax = Math.max(arr[i], preMin * arr[i]);
                curMin = Math.min(arr[i], preMax * arr[i]);
            }

            //update value
            maxProduct = Math.max(maxProduct, curMax);
            preMax = curMax;
            preMin = curMin;
        }

        return maxProduct;
    }

    public static void main(String[] args) {
        LC152 an = new LC152();
        int[] arr = {-2, 3, -4};
        System.out.println(an.maxProduct(arr));
    }
}
